// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// G [author] rhyme_lph
// J [email]  rhymelph@gmail.com
// N [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/bank_detail_record_item_entity.dart';
import 'package:finance_app/src/http_service/http_bank_reconciliations_service.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/bank_reconciliation_detail_with_file_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/dashboard_add_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:flutter/material.dart';

class ReconcileAccountRecordDetailProvider
    extends BaseProvider<List<BankDetailRecordItemEntity>> {
  String showStatus = 'Unmatched';
  int id;

  num realDifference;

  int bankId;

  num get difference {
    num difference = realDifference;
    num selectTotal = 0;
    selectReconcileList.forEach((element) {
      if (element.flowType == 'in') {
        selectTotal += element.total;
      } else {
        selectTotal -= element.total;
      }
    });
    return difference - selectTotal;
  }

  List<BankDetailRecordItemEntity> selectReconcileList = [];

  @override
  Future get dataFuture =>
      HttpBankReconciliationsService.paymentRecord(bankId, id, showStatus);

  void onSelectReconcile(BankDetailRecordItemEntity value) {
    final finder = selectReconcileList
        .firstWhere((element) => element.id == value.id, orElse: () => null);
    if (finder == null) {
      selectReconcileList.add(value);
    } else {
      selectReconcileList.removeWhere((element) => element.id == value.id);
    }
    update();
  }

  void onChangeStatus() {
    obtainContext?.call((context) async {
      final result =
          await showBankRecordsSelectBottomSheet(context, showStatus);
      if (result != null && showStatus != result) {
        showStatus = result;
        update();
        onRequestRefresh();
      }
    });
  }

  void onRequestRefresh() {
    post(dataFuture,
        autoHandler: AutoHandler.Toast,
        loadingTip: S.current.loading, onSuccess: (result) {
      data = getEntityFromResult<List<BankDetailRecordItemEntity>>(result);
      update();
    });
  }

  void onAddNew() {
    obtainContext?.call((context) async {
      showDashboardAddBottomSheet(context,
          dashTypes: [DashBoardAddType.accountTransaction]);
    });
  }

  void onMatch() async {
    List<int> ids = selectReconcileList.map((e) => e.id).toList();
    bool isSuccess = false;

    await post(HttpBankReconciliationsService.match(id, ids),
        loadingTip: S.current.loading,
        autoHandler: AutoHandler.Toast, onSuccess: (result) {
      isSuccess = true;
    });
    if (isSuccess == true) {
      obtainContext?.call((context) async {
        Navigator.of(context).pop(true);
        Providers.get<BankReconciliationDetailWithFileProvider>(
                RouterGenProviders.bankReconciliationDetailWithFilePage, null)
            ?.onRequestRefresh();
      });
    }
  }
}
